package com.entor.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import sun.beans.editors.DoubleEditor;
import sun.beans.editors.FloatEditor;
import sun.beans.editors.IntEditor;
import sun.beans.editors.LongEditor;

import com.alibaba.fastjson.JSON;
import com.entor.util.Json;
@Controller
public class BaseController<T> {
	
	/*@Resource
	private BaseServiceI<T> bsi*/;
	
	@InitBinder    
    public void initBinder(WebDataBinder binder) {    

        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); 
        binder.registerCustomEditor(int.class, new IntEditor());
        binder.registerCustomEditor(long.class, new LongEditor());  
        binder.registerCustomEditor(double.class, new DoubleEditor());  
        binder.registerCustomEditor(float.class, new FloatEditor());  
    }
	
	@RequestMapping("/{root}/{folder}/{jspName}")
	public String redirectJsp(@PathVariable String root,
			@PathVariable String folder,
			@PathVariable String jspName){
		return "/"+root+"/"+folder+"/"+jspName;
	}
	
	@RequestMapping("/find")
	@ResponseBody
	public List<T> find(T t){
		return null;
	}
	
	@RequestMapping("get")
	@ResponseBody
	public T get(T t){
		return null;
	}
	
	@RequestMapping("/{root}/{folder}/openDialog")
	public String openDialog(@PathVariable String root,
			@PathVariable String folder,
			HttpServletRequest request) throws Exception{
		String json = request.getParameter("json");
		if(json!=null){
			//json = new String(json.getBytes("iso-8859-1"),"utf-8");
			T t = (T) JSON.parseObject(json);
			request.setAttribute("t", t);
		}
		return "/"+root+"/"+folder+"/addOrEdit";
	}
	
	@RequestMapping("/insert")
	public String insert(T t){
		return null;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Json update(T t){
		return null;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(HttpServletRequest request){
		return null;
	}
	
	public Json getJson(int i,String type){
		Json j = new Json();
		if(i>0){
			j.setIsSuccess(true);
			j.setMsg(type+"成功");
		}else{
			j.setIsSuccess(false);
			j.setMsg(type+"失败");
		}
		return j;
	}

}
